package com.cskaoyan.service.goods;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cskaoyan.bean.bo.goods.*;
import com.cskaoyan.bean.po.goods.*;
import com.cskaoyan.bean.vo.goods.*;
import com.cskaoyan.mapper.goods.*;
import com.cskaoyan.mapper.goods.AdminGoodsAttributesDetailMapper;
import com.cskaoyan.util.beanutils.BeanUtil;
import com.cskaoyan.util.common.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.Data;
import net.sf.json.JSONArray;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: 冯子靖
 * @Description:
 * @Date: 2022/7/18 21:38
 */
@Service
@Data
public class AdminGoodsServiceImpl implements AdminGoodsService {
    private Integer limit;

    private Integer page;

    private Integer pages;

    private Integer total;

    private List<AdminGoodsListPO> list;

    @Autowired
    AdminGoodsMapper adminGoodsMapper;

    @Autowired
    AdminGoodsBrandMapper adminGoodsBrandMapper;

    @Autowired
    AdminGoodsCategoryMapper adminGoodsCategoryMapper;

    @Autowired
    AdminCreateGoodsMapper adminCreateGoodsMapper;

    @Autowired
    AdminCreateAttributesMapper adminCreateAttributesMapper;

    @Autowired
    AdminCreateProductsMapper adminCreateProductsMapper;

    @Autowired
    AdminCreateSpecificationsMapper adminCreateSpecificationsMapper;

    @Autowired
    AdminGoodsAttributesDetailMapper adminGoodsAttributesDetailMapper;

    @Autowired
    AdminGoodsGoodsDetailMapper adminGoodsGoodsDetailMapper;

    @Autowired
    AdminGoodsProductsDetailMapper adminGoodsProductsDetailMapper;

    @Autowired
    AdminGoodsSpecificationsDetailMapper adminGoodsSpecificationsDetailMapper;

    @Autowired
    AdminGoodsCategoryDetailMapper adminGoodsCategoryDetailMapper;

    @Autowired
    AdminUpdateAttributesMapper adminUpdateAttributesMapper;

    @Autowired
    AdminGoodsGoodsUpdateMapper adminGoodsGoodsUpdateMapper;

    @Autowired
    AdminGoodsProductsUpdateMapper adminGoodsProductsUpdateMapper;

    @Autowired
    AdminGoodsSpecificationsUpdateMapper adminGoodsSpecificationsUpdateMapper;

    @Autowired
    AdminGoodsGoodsDeleteMapper adminGoodsGoodsDeleteMapper;

    @Autowired
    AdminGoodsAttributesDeleteMapper adminGoodsAttributesDeleteMapper;

    @Autowired
    AdminGoodsProductsDeleteMapper adminGoodsProductsDeleteMapper;

    @Autowired
    AdminGoodsSpecificationsDeleteMapper adminGoodsSpecificationsDeleteMapper;

    @Override
    public AdminGoodsListVO printAdminGoodsList(AdminGoodsBO adminGoodsBO) {
        page = adminGoodsBO.getPage();
        limit = adminGoodsBO.getLimit();

        Integer goodsId = adminGoodsBO.getGoodsId();
        String goodsSn = adminGoodsBO.getGoodsSn();
        String name = adminGoodsBO.getName();
        AdminGoodsListVO adminGoodsListVO = new AdminGoodsListVO();

        if (limit != 0) {
            PageHelper.startPage(page, limit);
            QueryWrapper<AdminGoodsListPO> adminUserListPOQueryWrapper = new QueryWrapper<>();
            adminUserListPOQueryWrapper.eq("deleted", 0);
            adminUserListPOQueryWrapper.orderByDesc("add_time");
            if (goodsId != null || !StringUtils.isEmpty(goodsSn) || !StringUtils.isEmpty(name)) {

                adminUserListPOQueryWrapper.eq(!(goodsId == null), "id", goodsId);
                adminUserListPOQueryWrapper.eq(!StringUtils.isEmpty(goodsSn), "goods_sn", goodsSn);
                adminUserListPOQueryWrapper.like(!StringUtils.isEmpty(name), "name", name);
            }
            list = adminGoodsMapper.selectList(adminUserListPOQueryWrapper);

            PageInfo<AdminGoodsListPO> adminUserAddressPOPageInfo = new PageInfo<>(list);
            total = (int) adminUserAddressPOPageInfo.getTotal();
            pages = adminUserAddressPOPageInfo.getPages();
            BeanUtil.copyBean(this, adminGoodsListVO);
            return adminGoodsListVO;
        } else {
            this.page = 0;
            this.limit = 0;
            QueryWrapper<AdminGoodsListPO> adminUserListPOQueryWrapper = new QueryWrapper<>();
            adminUserListPOQueryWrapper.eq("deleted", 0);
            adminUserListPOQueryWrapper.orderByDesc("add_time");
            if (goodsId != null || !StringUtils.isEmpty(goodsSn) || !StringUtils.isEmpty(name)) {

                adminUserListPOQueryWrapper.eq(!(goodsId == null), "id", goodsId);
                adminUserListPOQueryWrapper.eq(!StringUtils.isEmpty(goodsSn), "goods_sn", goodsSn);
                adminUserListPOQueryWrapper.like(!StringUtils.isEmpty(name), "name", name);
            }
            list = adminGoodsMapper.selectList(adminUserListPOQueryWrapper);

            this.total = 0;
            this.pages = 0;
            BeanUtil.copyBean(this, adminGoodsListVO);
            return adminGoodsListVO;
        }
    }

    @Override
    public AdminGoodsCatAndBrandVO printAdminGoodsCatAndBrand() {
        /**
         * 获取brandList
         */
        List<AdminGoodsBrandPO> brandList = adminGoodsBrandMapper.selectList(null);
        List<AdminGoodsBrandVO> brandListVO = new ArrayList<>();
        for (AdminGoodsBrandPO brand : brandList) {
            AdminGoodsBrandVO adminGoodsBrandVO = new AdminGoodsBrandVO();
            BeanUtil.copyBean(brand, adminGoodsBrandVO);
            brandListVO.add(adminGoodsBrandVO);
        }
        /**
         * 获取categoryList = childrenMap + label + value
         */
        ArrayList<AdminGoodsCategoryListVO> adminGoodsCategoryList = new ArrayList<>();

        QueryWrapper<AdminGoodsCategoryPO> adminGoodsCategoryPOQueryWrapper = new QueryWrapper<>();
        //查询父类
        adminGoodsCategoryPOQueryWrapper.eq("pid", 0);
        List<AdminGoodsCategoryPO> categoryList = adminGoodsCategoryMapper.selectList(adminGoodsCategoryPOQueryWrapper);
        //查询子类
        for (AdminGoodsCategoryPO cat : categoryList) {
            QueryWrapper<AdminGoodsCategoryPO> faWrapper = new QueryWrapper<>();
            faWrapper.eq("pid", cat.getId());
            List<AdminGoodsCategoryPO> childrenList = adminGoodsCategoryMapper.selectList(faWrapper);
            List<AdminGoodsBrandVO> adminGoodsBrandVOS = new ArrayList<>();
            for (AdminGoodsCategoryPO children : childrenList) {
                AdminGoodsBrandVO adminGoodsBrandVO = new AdminGoodsBrandVO();
                BeanUtil.copyBean(children, adminGoodsBrandVO);
                adminGoodsBrandVOS.add(adminGoodsBrandVO);
            }


            AdminGoodsCategoryListVO adminGoodsCategoryListVO = new AdminGoodsCategoryListVO();
            BeanUtil.copyBean(cat, adminGoodsCategoryListVO);
            adminGoodsCategoryListVO.setChildren(adminGoodsBrandVOS);
            adminGoodsCategoryList.add(adminGoodsCategoryListVO);

        }
        return new AdminGoodsCatAndBrandVO(brandListVO, adminGoodsCategoryList);
    }

    @Override
    public void insertGoods(AdminGoodsCreateBO goodsCreate) {
        CreateGoodsBO goods = goodsCreate.getGoods();
        Date date = new Date();
        goods.setAddTime(date);
        goods.setUpdateTime(date);
        adminCreateGoodsMapper.insert(goods);
        //获得插入的商品id
        Integer goodsId = goods.getId();
        //插入att
        List<CreateAttributesBO> attributes = goodsCreate.getAttributes();
        for (CreateAttributesBO attribute : attributes) {
            attribute.setGoodsId(goodsId);

            attribute.setAddTime(date);
            attribute.setUpdateTime(date);
            adminCreateAttributesMapper.insert(attribute);
        }


        //插入pro
        List<CreateProductsBO> products = goodsCreate.getProducts();
        for (CreateProductsBO product : products) {
            product.setGoodsId(goodsId);
            product.setAddTime(date);
            product.setUpdateTime(date);
            adminCreateProductsMapper.insert(product);
        }


        //插入spec
        List<CreateSpecificationsBO> specifications = goodsCreate.getSpecifications();
        for (CreateSpecificationsBO specificationsBO : specifications) {
            specificationsBO.setGoodsId(goodsId);
            specificationsBO.setAddTime(date);
            specificationsBO.setUpdateTime(date);
            adminCreateSpecificationsMapper.insert(specificationsBO);
        }

    }

    @Override
    public AdminGoodsDetailVO showGoodsDetail(Integer id) {
        //查询att
        QueryWrapper<AdminGoodsAttributesDetailPO> adminGoodsAttributesDetailPOQueryWrapper = new QueryWrapper<>();
        adminGoodsAttributesDetailPOQueryWrapper.eq("goods_id", id);
        adminGoodsAttributesDetailPOQueryWrapper.eq("deleted", 0);

        List<AdminGoodsAttributesDetailPO> attributesDetails = adminGoodsAttributesDetailMapper.selectList(adminGoodsAttributesDetailPOQueryWrapper);
        //查询goods
        AdminGoodsGoodsDetailPO goods = adminGoodsGoodsDetailMapper.selectById(id);
        //查询cat
        Integer categoryId = goods.getCategoryId();
        QueryWrapper<AdminGoodsCategoryDetailPO> integerQueryWrapper = new QueryWrapper<>();
        integerQueryWrapper.eq("id", categoryId);
        AdminGoodsCategoryDetailPO adminGoodsCategoryDetailPO = adminGoodsCategoryDetailMapper.selectOne(integerQueryWrapper);

        List<Integer> integers = new ArrayList<>();

        integers.add(adminGoodsCategoryDetailPO.getPid());
        integers.add(categoryId);
        //查询pro
        QueryWrapper<AdminGoodsProductsDetailPO> adminGoodsProductsDetailPOQueryWrapper = new QueryWrapper<>();
        adminGoodsProductsDetailPOQueryWrapper.eq("goods_id", id);

        List<AdminGoodsProductsDetailPO> adminGoodsProductsDetails = adminGoodsProductsDetailMapper.selectList(adminGoodsProductsDetailPOQueryWrapper);
        //查询spec
        QueryWrapper<AdminGoodsSpecificationsDetailPO> adminGoodsSpecificationsDetailPOQueryWrapper = new QueryWrapper<>();
        adminGoodsSpecificationsDetailPOQueryWrapper.eq("goods_id", id);

        List<AdminGoodsSpecificationsDetailPO> adminGoodsSpecificationsDetails = adminGoodsSpecificationsDetailMapper.selectList(adminGoodsSpecificationsDetailPOQueryWrapper);
        //转换成VO
        List<AdminGoodsAttributesDetailVO> attributes = new ArrayList<>();
        for (AdminGoodsAttributesDetailPO attributesDetail : attributesDetails) {
            AdminGoodsAttributesDetailVO adminGoodsAttributesDetailVO = new AdminGoodsAttributesDetailVO();
            BeanUtil.copyBean(attributesDetail, adminGoodsAttributesDetailVO);
            attributes.add(adminGoodsAttributesDetailVO);
        }

        List<AdminGoodsSpecificationsDetailVO> specifications = new ArrayList<>();

        for (AdminGoodsSpecificationsDetailPO adminGoodsSpecificationsDetail : adminGoodsSpecificationsDetails) {
            AdminGoodsSpecificationsDetailVO adminGoodsSpecificationsDetailVO = new AdminGoodsSpecificationsDetailVO();
            BeanUtil.copyBean(adminGoodsSpecificationsDetail, adminGoodsSpecificationsDetailVO);
            specifications.add(adminGoodsSpecificationsDetailVO);
        }
        AdminGoodsDetailVO detail = new AdminGoodsDetailVO();
        detail.setAttributes(attributes);
        detail.setCategoryIds(integers);
        detail.setGoods(goods);
        detail.setProducts(adminGoodsProductsDetails);

        detail.setSpecifications(specifications);
        return detail;
    }

    /**
     * 更新商品
     *
     * @param adminGoodsUpdateBO
     */
    @Override
    public void updateGoods(AdminGoodsUpdateBO adminGoodsUpdateBO) {

        Date date = new Date();

        List<AdminGoodsAttributesUpdateBO> attributes = adminGoodsUpdateBO.getAttributes();
        AdminGoodsGoodsUpdateBO good = adminGoodsUpdateBO.getGoods();
        Integer goodsId = good.getId();
        List<AdminGoodsProductsUpdateBO> products = adminGoodsUpdateBO.getProducts();
        List<AdminGoodsSpecificationsUpdateBO> specifications = adminGoodsUpdateBO.getSpecifications();

        /*
        更新商品attributes
         */
        for (AdminGoodsAttributesUpdateBO attribute : attributes) {
            Integer attributeId = attribute.getId();
            if (attributeId == null) {
                //插入att
                attribute.setGoodsId(goodsId);
                AdminGoodsAttributesUpdatePO adminGoodsAttributesUpdatePO = new AdminGoodsAttributesUpdatePO();
                try {
                    BeanUtils.copyProperties(adminGoodsAttributesUpdatePO, attribute);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
                adminGoodsAttributesUpdatePO.setAddTime(date);
                adminGoodsAttributesUpdatePO.setUpdateTime(date);

                adminUpdateAttributesMapper.insert(adminGoodsAttributesUpdatePO);
            } else {
                //更新已有att
                AdminGoodsAttributesUpdatePO adminGoodsAttributesUpdatePO = new AdminGoodsAttributesUpdatePO();
                try {
                    BeanUtils.copyProperties(adminGoodsAttributesUpdatePO, attribute);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
                adminUpdateAttributesMapper.updateById(adminGoodsAttributesUpdatePO);
            }
        }
        /*
        更新商品goods
         */
        AdminGoodsGoodsUpdatePO adminGoodsGoodsUpdatePO = new AdminGoodsGoodsUpdatePO();
        try {
            BeanUtils.copyProperties(adminGoodsGoodsUpdatePO, good);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        adminGoodsGoodsUpdatePO.setUpdateTime(date);
        String s = JSONArray.fromObject(good.getGallery()).toString();

        adminGoodsGoodsUpdatePO.setGallery(s);
        adminGoodsGoodsUpdateMapper.updateById(adminGoodsGoodsUpdatePO);

        /*
        更新商品products
         */

        for (AdminGoodsProductsUpdateBO product : products) {
            AdminGoodsProductsUpdatePO adminGoodsProductsUpdatePO = new AdminGoodsProductsUpdatePO();
            try {
                BeanUtils.copyProperties(adminGoodsGoodsUpdatePO, product);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            adminGoodsProductsUpdatePO.setUpdateTime(date);

            adminGoodsProductsUpdateMapper.updateById(adminGoodsProductsUpdatePO);
        }

        /*
        更新specifications
         */

        for (AdminGoodsSpecificationsUpdateBO specification : specifications) {
            AdminGoodsSpecificationsUpdatePO adminGoodsSpecificationsUpdatePO = new AdminGoodsSpecificationsUpdatePO();
            try {
                BeanUtils.copyProperties(adminGoodsSpecificationsUpdatePO, specification);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            adminGoodsSpecificationsUpdatePO.setUpdateTime(date);

            adminGoodsSpecificationsUpdateMapper.updateById(adminGoodsSpecificationsUpdatePO);
        }
    }

    @Override
    public void deleteGoods(AdminGoodsDeleteBO adminGoodsDeleteBO) {
        Integer goodsId = adminGoodsDeleteBO.getId();
        Date date = new Date();
        /*
        删除商品
         */
        AdminGoodsDeletePO adminGoodsDeletePO = new AdminGoodsDeletePO();
        try {
            BeanUtils.copyProperties(adminGoodsDeletePO, adminGoodsDeleteBO);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        adminGoodsDeletePO.setUpdateTime(date);
        adminGoodsDeletePO.setDeleted(true);
        adminGoodsGoodsDeleteMapper.updateById(adminGoodsDeletePO);

        /*
        删除att
         */
        AdminGoodsAttributesDeletePO adminGoodsAttributesDeletePO = new AdminGoodsAttributesDeletePO();
        adminGoodsAttributesDeletePO.setUpdateTime(date);
        adminGoodsAttributesDeletePO.setDeleted(true);
        QueryWrapper<AdminGoodsAttributesDeletePO> adminGoodsAttributesDeletePOQueryWrapper = new QueryWrapper<>();
        adminGoodsAttributesDeletePOQueryWrapper.eq("goods_id", goodsId);
        adminGoodsAttributesDeleteMapper.update(adminGoodsAttributesDeletePO, adminGoodsAttributesDeletePOQueryWrapper);

        /*
        删除product
         */
        AdminGoodsProductsDeletePO adminGoodsProductsDeletePO = new AdminGoodsProductsDeletePO();
        adminGoodsProductsDeletePO.setDeleted(true);
        adminGoodsProductsDeletePO.setUpdateTime(date);
        QueryWrapper<AdminGoodsProductsDeletePO> adminGoodsProductsDeletePOQueryWrapper = new QueryWrapper<>();
        adminGoodsProductsDeletePOQueryWrapper.eq("goods_id", goodsId);
        adminGoodsProductsDeleteMapper.update(adminGoodsProductsDeletePO, adminGoodsProductsDeletePOQueryWrapper);

        /*
        删除specification
         */
        AdminGoodsSpecificationsDeletePO adminGoodsSpecificationsDeletePO = new AdminGoodsSpecificationsDeletePO();
        adminGoodsSpecificationsDeletePO.setDeleted(true);
        adminGoodsSpecificationsDeletePO.setUpdateTime(date);
        QueryWrapper<AdminGoodsSpecificationsDeletePO> adminGoodsSpecificationsDeletePOQueryWrapper = new QueryWrapper<>();
        adminGoodsSpecificationsDeletePOQueryWrapper.eq("goods_id", goodsId);
        adminGoodsSpecificationsDeleteMapper.update(adminGoodsSpecificationsDeletePO, adminGoodsSpecificationsDeletePOQueryWrapper);
    }


}
